#  获取所有安全组的白名单IP地址并打印
import boto3

def get_all_security_group_white_list(region):
    ec2 = boto3.client('ec2', region_name=region)
    response = ec2.describe_security_groups()

    # 使用集合存储IP地址，确保去重
    white_list_ips = set()

    for security_group in response['SecurityGroups']:
        for permission in security_group['IpPermissions']:
            for ip_range in permission.get('IpRanges', []):
                cidr_ip = ip_range['CidrIp']
                # 将IP地址添加到白名单集合中
                white_list_ips.add(cidr_ip)

    # 打印白名单IP地址
    print("所有安全组的白名单IP地址:")
    for ip in white_list_ips:
        print(ip)

# 替换为你自己的AWS区域
region = 'ap-northeast-1'

# 获取所有安全组的白名单IP地址并打印
get_all_security_group_white_list(region)